package catchData.ahyycg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

import util.DateGroup;

/**
 * @author suwy
 * 2013-1-4
 * TODO data from 安徽省医药集中采购平台 <查看>
 */
public class Catch4ahyycgMore {
		private static Logger log = Logger.getLogger("Catch4ahyycgMore");
		private static HttpClient client;
//		private static final String LOGIN_PAGE = "http://www.ahyycg.cn/NoticeBoard/GoodsList_hos.aspx";
		private static final String LOGIN_PAGE = "http://www.ahyycg.cn/NoticeBoard/";
		private static final String WEB_NAME = "GoodsList_hos.aspx";
		private static final String HOST = "www.ahyycg.cn";
		private static final int pageNum = 2;
		private static String path = "E:/downloadData/安徽省医药集中采购平台/"+WEB_NAME+"-2013.1.7";

		private void initHttpClient() {
			if (client == null) {
				MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
				client = new HttpClient(connectionManager);
				// 配置服务器参量
				client.getParams().setCookiePolicy(
						CookiePolicy.BROWSER_COMPATIBILITY); // 配置Cookie策略为浏览器默认策略
				client.getParams().setParameter(
						"http.protocol.single-cookie-header", true);
				client.getParams().setParameter(
						HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
				client.getHttpConnectionManager().getParams().setConnectionTimeout(
						300000);
				client.getHttpConnectionManager().getParams().setSoTimeout(300000);
			}
		}

		private void setHeaders(HttpMethod method) {
			method.getParams().setParameter("http.protocol.cookie-policy",
					CookiePolicy.BROWSER_COMPATIBILITY);
			method.setRequestHeader("accept",
							"image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
			method.setRequestHeader("referer", LOGIN_PAGE+WEB_NAME);
			method.setRequestHeader("accept-encoding",
					"UTF-8,GBK,GB2312,gzip,deflate");
			method.setRequestHeader("accept-language", "zh-CN");
			method.setRequestHeader("content-type",
					"application/x-www-form-urlencoded");
			method.setRequestHeader(
							"user-agent",
							"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPUY4RDgxQUY2MTgwQjQ3REQ5MUI5QkM4NjU3MzAwRkU3JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=b226e2d473bb8dbbb37568db14fee0e4; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9TcGVjaWFsLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAxMC42LjMuNS4yJmNyYXNodHlwZT0x&verify=94c65611a34cfb4964638346b072d76e; staticlogin:product=cboxf09&act=login&info=ZmlsZW5hbWU9UG93ZXJ3b3JkMjAwOU94Zi4yNTI2OS40MDExLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAwOS4wNS4yNS4zLjI3MiZjcmFzaHR5cGU9MQ==&verify=32e210faa8ce04f90dfc4225f98e80ac; aff-kingsoft-ciba; TheWorld)");
			method.setRequestHeader("host", HOST);
			method.setRequestHeader("connection", "Keep-Alive");
			method.setRequestHeader("cache-control", "no-cache");
		}

		private void saveDataFile(String sFolder, String data, long lid) {
			saveDataFile(sFolder, data, lid + "");
		}

		private void saveDataFile(String sFolder, String data, String sid) {
			File folder = new File(sFolder);
			if (!folder.exists()) folder.mkdirs();
			PrintWriter out = null;
			String saveFile = sFolder + "/" + sid + ".htm";
			try {
				out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
						saveFile), "GBK"));
				out.println(data);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.close();
						out = null;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				log.info("【" + DateGroup.getNow() + "】 保存文件成功！"
						+ saveFile.replaceAll("\\\\", "/"));
			}
		}


		public static void main(String[] args) {
			String regex="<a href='(.*?)' target=\"_blank\" ><span style=\"color:#49A3FE;\">查看</span></a>";
			int seq = 0;
			String str = null;
			String str2 = null;
			PostMethod post = null;
			PostMethod post2 = null;
			Pattern pattern = null;
			Matcher matcher = null;
			try {
				Catch4ahyycgMore instance = new Catch4ahyycgMore();
				instance.initHttpClient();
				StringBuffer id = new StringBuffer();
				for (int i = 1; i <= pageNum; i++) {
					post = new PostMethod(LOGIN_PAGE+WEB_NAME);
					instance.setHeaders(post);
					NameValuePair eventTarget = new NameValuePair("__EVENTTARGET",
							"ctl00$ContentPlaceHolder1$pager1");
					NameValuePair eventArgument = new NameValuePair("__EVENTARGUMENT", i + "");
					post.setRequestBody(new NameValuePair[] { eventTarget, eventArgument });
					client.executeMethod(post);
					str = post.getResponseBodyAsString();
					pattern = Pattern.compile(regex);
					matcher = pattern.matcher(str);
					
					while (matcher.find()) {
						id.append("&&&" + matcher.group().toString().replace("<a href='", "").replace("' target=\"_blank\" ><span style=\"color:#49A3FE;\">查看</span></a>", "").trim());
					}
					String[] ids = id.toString().split("&&&");
					for(int k = 1; k < ids.length; k++){
						post2 = new PostMethod(LOGIN_PAGE+ids[k]);
						instance.setHeaders(post2);
						client.executeMethod(post2);
						str2 = post2.getResponseBodyAsString();
//						System.out.println(str2);
						instance.saveDataFile(path, str2, ++seq);
						post2.releaseConnection();
					}
					post.releaseConnection();
					id.setLength(0);
					log.info("【next page 】is " + (i+1));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}